//============================================================
//  simulator function and task
//============================================================
//------------------------------------------------------------
// generate random 32
//------------------------------------------------------------
function automatic [TW-1:0] rand_w();
   reg [7:0] r[4];
   for(integer i=0;i<4;i=i+1)
         r[i]=$random(seed);

   rand_w={
           r[ 3],r[ 2],r[ 1],r[ 0]
          };
   endfunction
//------------------------------------------------------------
// generate random 64
//------------------------------------------------------------
function automatic [TD-1:0] rand_d();
   reg [7:0] r[8];
   for(integer i=0;i<8;i=i+1)
         r[i]=$random(seed);

   rand_d={
           r[ 7],r[ 6],r[ 5],r[ 4],
           r[ 3],r[ 2],r[ 1],r[ 0]
          };
   endfunction
//------------------------------------------------------------
// generate random 128
//------------------------------------------------------------
function automatic [TQ-1:0] rand_q();
   reg [7:0] r[16];
   for(integer i=0;i<16;i=i+1)
         r[i]=$random(seed);

   rand_q={r[15],r[14],r[13],r[12],
           r[11],r[10],r[ 9],r[ 8],
           r[ 7],r[ 6],r[ 5],r[ 4],
           r[ 3],r[ 2],r[ 1],r[ 0]
          };
   endfunction
//------------------------------------------------------------
// display succeed
//------------------------------------------------------------
task automatic display_succeed;
	begin
	if(succeed==1)
		$display("######## SUCCEED >> seed:%h  is succeed",s_seed);
	else    begin
		$display("######## ERROR  >>  seed:%h  is error  ",s_seed);
                end
        run_off<=1;
        @(posedge clk);
	$finish;
	end
endtask
//------------------------------------------------------------
// seed load
//------------------------------------------------------------
task automatic load_seed;
   begin
	  integer m[0:1];
	  integer fp;
	  integer r;
	  $readmemh("seed.dat",m);
	  seed=m[0];
	  if(seed===32'dx) begin
		 seed=$random;
         $display("###### SEED:seed.dat is not exist, using init seed %h",seed);
      end
	  
	  s_seed=seed;
`ifdef JZ4785_FXU_SEED
      seed=seed+$random;
	  fp=$fopen("seed.dat","w");
	  $fdisplayh(fp,seed);
	  $fclose(fp);
      $write("###### SEED:using variable seed,if needs static seed from file,");
      $write("undef JZ4785_FXU_SEED\n");
`else
      $write("###### SEED:using static seed,if needs variable seed,");
      $write("define JZ4785_FXU_SEED\n");
`endif
      $display("seed is %h",s_seed);
   end
endtask

